package beans.net;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IpDNSTools
{

	public static void main(String[] args)
	{
		System.out.println("MY PC IP is : " + getMyIP());
		System.out.println("The Server IP is : " + getServerIP());

		for (InetAddress address : getServerIP2())
		{
			System.out.println(address.getHostAddress());
		}

	}

	// 取得本地的IP地址
	public static InetAddress getMyIP()
	{
		InetAddress myIPaddress = null;
		try
		{
			myIPaddress = InetAddress.getLocalHost();
		}
		catch (UnknownHostException e)
		{
		}
		return (myIPaddress);
	}

	// 取得 制定 host的IP地址
	public static InetAddress getServerIP()
	{
		InetAddress myServer = null;
		try
		{
			myServer = InetAddress.getByName("www.abc.com");
		}
		catch (UnknownHostException e)
		{
		}
		return (myServer);
	}

	// 一个域名对应多个IP地址
	public static InetAddress[] getServerIP2()
	{
		InetAddress[] addresses = null;
		try
		{
			addresses = InetAddress.getAllByName("www.sina.com");
		}
		catch (UnknownHostException e)
		{
		}
		return addresses;
	}

}
